package com.zm.zmgz.configure;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.zm.zmgz.dao",sqlSessionFactoryRef = "sqlSessionFactory",sqlSessionTemplateRef =
        "sqlSessionTemplate")
public class MybaitsConfig {
    @Resource(name = "dataSource")
    DataSource dataSource;

    @Bean
    SqlSessionFactory sqlSessionFactory(){
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new  PathMatchingResourcePatternResolver();
//        bean.setMapperLocations(resolver.getResource("classpath:/mapper/UserDao.xml"));
        try {
            SqlSessionFactory factory = bean.getObject();
            return factory;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    SqlSessionTemplate sqlSessionTemplate(){
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
        return sqlSessionTemplate;
    }
}
